package com.training.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.training.dto.Employee;

@Controller
public class LoginController {

	@RequestMapping(value = "/login.do", method = RequestMethod.POST)
	public ModelAndView login(HttpServletRequest request,
			HttpServletResponse response)

	{
		ModelAndView mav = new ModelAndView();

		if ((request.getParameter("empID") != null && request
				.getParameter("empID").trim().equalsIgnoreCase("spring"))
				&& (request.getParameter("password") != null && request
						.getParameter("password").trim()
						.equalsIgnoreCase("password"))) {

			Employee employee = new Employee();
			employee.setEmpId("100");
			employee.setFirstName("spring");
			employee.setLastName("MVC");

			mav.addObject("empl", employee);
			mav.setViewName("home.jsp");

			return mav;

		} else {
			mav.addObject("errorMessage",
					"Please Enter Valid UserName /Password");
			mav.setViewName("login.jsp");
			return mav;

		}
	}

	@RequestMapping(value = "/register.do")
	public ModelAndView registerUser(HttpServletRequest request,
			HttpServletResponse response) {

		ModelAndView modelAndView = new ModelAndView();

		modelAndView.setViewName("welcome.jsp");
		return modelAndView;

	}

}